package cn.dlc.com;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ScanResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 邓立川
 * @date 2023/11/7 23:01
 * @desc 当前类的描述信息
 */
public class DemoTest1 {

    private Jedis jedis;

    @Before
    private void init(){
        // 创建连接
        jedis = new Jedis("192.168.31.100", 6379);
        // 如果有密码的话
        // jedis.auth("");

        // 选择数据库
        jedis.select(0);
    }

    @Test
    public void testString(){
        String set = jedis.set("name", "jack");
    }

    @After
    public void close(){
        if(jedis != null) {
            jedis.close();
        }
    }


    final static int STR_MAX_LEN = 10*1024; // 最长10kb
    final static int HASH_MAX_LEN = 500; // hash最多500个元素
    @Test
    public void testScan() {
        long maxLen = 0;
        long len = 0;

        String cursor = "0"; // 扫描开始的指针
        do {
            // 扫描一部分的key
            ScanResult<String> scan = jedis.scan(cursor);

            // 扫描到的位置
            cursor = scan.getCursor();

            // 扫描出来的key
            List<String> result = scan.getResult();
            if(result != null && !result.isEmpty()) {
                for (String key : result) {
                    // 获取key的类型
                    String type = jedis.type(key);
                    switch (type) {
                        case "string":
                            len = jedis.strlen(key);
                            maxLen = STR_MAX_LEN;
                            break;
                        case "hash":
                            len = jedis.hlen(key);
                            maxLen = HASH_MAX_LEN;
                            break;
                        case "list":
                            len = jedis.llen(key);
                            maxLen = HASH_MAX_LEN;
                            break;
                        case "set":
                            len = jedis.scard(key);
                            maxLen = HASH_MAX_LEN;
                            break;
                        case "zset":
                            len = jedis.zcard(key);
                            maxLen = HASH_MAX_LEN;
                            break;
                    }
                    if(len > maxLen) {
                        System.out.println("这个是一个bigKey");
                    }
                }
            }
        } while(!cursor.equals("0"));
    }


    @Test
    public void testPipeline(){
        Pipeline pipelined = jedis.pipelined();
        // 放一条string的
        pipelined.set("key","value");
        // 放一条hset的
        pipelined.hset("key", "field", "value");
        // 执行pipeline
        pipelined.sync();
    }


    @Test
    public void testSlot(){
        Map<String, String> map = new HashMap<>();
        map.put("name","jack");
        map.put("age","12");
        map.put("sex","male");

        map.entrySet().stream().collect(Collectors.groupingBy(entry->ClusterSlotHashUtil.))
    }



}
